package operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class RemoveOperation implements Operation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书->");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要删除的书名->");
        String name = scan.next();
        int tmp = bookList.getUsedSize();
        int flag = -1;
        for (int i = 0; i < tmp; i++) {
            Book book = bookList.getBooks(i);
            if(name.equals(book.getName())) {
                flag = i;
            }
        }
        if(flag == -1) {
            System.out.println("没有这本书，删除失败！");
            return;
        }

        for (int i = flag; i < tmp-1; i++) {
            bookList.setBooks(bookList.getBooks(i+1),i);
        }
        bookList.setBooks(null, tmp-1);
        bookList.setUsedSize(tmp-1);
        System.out.println("删除成功！");
    }
}
